<template>
  <u-popup
    mode="bottom"
    v-model="showPopup"
    :z-index="999"
    safe-area-inset-bottom
    :maskCloseAble="true"
  >
    <view class="share-section">
      <view
        class="share-content"
        :style="{ paddingTop: 'calc(' + statusBarHeight + 'px + 88rpx);' }"
      >
        <view class="close-section" @click="showPopup = false">
          <i class="iconfont icon-arrow-down"></i>
          <view>{{ $t("common.close") }}</view>
        </view>
        <view class="to-friend">{{ $t("diy.recommend") }}</view>
        <view class="poster-img">
          <image
            class="share-img"
            v-if="imgshow"
            :src="shareImg"
            mode="aspectFill"
            @click="previewImg"
          />
          <view class="download-box" v-if="imgshow" @click="saveEwm">
            <i class="iconfont icon-download"></i>
            <button
              v-if="!openSettingBtnHidden"
              class="open-btn"
              hover-class="none"
              open-type="openSetting"
              @opensetting="handleSetting"
            >
              {{ $t("common.album") }}
            </button>
          </view>
        </view>
        <view class="poster-down-text">{{ $t("diy.share") }}</view>
        <view class="share-btn-group">
          <view
            class="share-btn-item"
            v-for="(item, index) in shareList"
            :key="index"
            @click="handleToShare(item)"
          >
            <view class="item-icon-box">
              <i
                class="iconfont"
                :style="{ color: item.color }"
                :class="item.icon"
              ></i>
            </view>
            <view class="item-text">{{ item.text }}</view>
            <button
              v-if="item.type == 'wexin'"
              hover-class="none"
              class="wxshareBtn"
              open-type="share"
            ></button>
          </view>
        </view>
        <view class="safe"></view>
      </view>
      <view class="share-bg" @click="showPopup = false">
        <view class="grey-bg"></view>
        <view class="white-bg"></view>
      </view>
    </view>
  </u-popup>
</template>

<script>
import { diySysPageMixin } from "@/components/diySystemPage/mixins/index";

export default {
  name: "",
  props: {
    shareImg: {
      type: String,
      default: "",
    },
  },
  mixins: [diySysPageMixin],
  components: {},
  data() {
    return {
      showPopup: false,

      shareList: [
        {
          icon: "icon-wechat",
          text: "微信",
          type: "wexin",
          color: "#08BB0A",
        },
      ],

      appShare: [
        {
          type: this.$t("common.shareFriend"),
          pic_url: "share_wx",
          id: 1,
        },
        {
          type: this.$t("common.beforePoster"),
          pic_url: "share_qrcode",
          id: 2,
        },
      ],

      show: false,
      poster_show: false,
      imgshow: false,
      openSettingBtnHidden: true, //是否授权
    };
  },

  methods: {
    handleToShare(item) {
      if (item.type == "wexin") {
        this.shareWx();
      }
      if (item.type == "wxgroup") {
        this.shareTo("WXSenceTimeline");
      }
    },

    shareTo(type) {
      let { shareImg: imageUrl } = this;
      switch (type) {
        case "WXSceneSession":
          uni.share({
            provider: "weixin",
            scene: "WXSceneSession",
            type: 2,
            imageUrl,
          });
          break;

        case "WXSenceTimeline":
          uni.share({
            provider: "weixin",
            scene: "WXSenceTimeline",
            type: 2,
            imageUrl,
          });
          break;
      }
    },

    //微信小程序保存到相册
    handleSetting(e) {
      if (!e.detail.authSetting["scope.writePhotosAlbum"]) {
        this.openSettingBtnHidden = false;
      } else {
        this.openSettingBtnHidden = true;
      }
    },

    //获取授权
    saveEwm() {
      // #ifndef APP-PLUS
      //获取相册授权
      uni.getSetting({
        success: (res) => {
          if (!res.authSetting["scope.writePhotosAlbum"]) {
            uni.authorize({
              scope: "scope.writePhotosAlbum",
              success: () => {
                //这里是用户同意授权后的回调
                this.saveImgToLocal();
              },
              fail: () => {
                //这里是用户拒绝授权后的回调
                this.openSettingBtnHidden = false;
              },
            });
          } else {
            //用户已经授权过了
            this.saveImgToLocal();
          }
        },
      });
      // #endif
      // #ifdef APP-PLUS
      this.saveImgToLocal();
      // #endif
    },

    //下载并保存图片
    saveImgToLocal() {
      uni.showModal({
        title: this.$t("order.prompt"),
        content: this.$t("common.shareContent"),
        success: (res) => {
          if (res.confirm) {
            uni.downloadFile({
              url: this.shareImg, //图片地址
              success: (res) => {
                if (res.statusCode === 200) {
                  uni.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: () => {
                      uni.showToast({
                        title: this.$t("common.saveSuc"),
                        icon: "none",
                      });
                    },
                    fail: () => {
                      uni.showToast({
                        title: this.$t("common.saveFail"),
                        icon: "none",
                      });
                    },
                  });
                }
              },
              fail: () => {
                uni.showToast({
                  title: "保存失败，请检查是否开启权限",
                  icon: "none",
                });
              },
            });
          }
        },
      });
    },

    shareWx() {
      uni.share({
        provider: "weixin",
        type: 2,
        imageUrl: this.shareImg,
      });
    },

    // 点击预览图片
    previewImg() {
      uni.previewImage({
        current: "0",
        urls: [this.shareImg],
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.share-section {
  width: 100%;
  height: 100vh;

  .share-bg {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    .grey-bg {
      width: 100%;
      height: 63.48vh;
      background: #000000;
      opacity: 0.5;
    }
    .white-bg {
      flex: 1;
      background-color: #f2f2f2;
    }
  }
  .share-content {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 12.665vw;
    top: 0;
    z-index: 11;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    padding-bottom: 4.8vw;
    width: 74.67vw;
    .safe {
      padding-bottom: env(safe-area-inset-bottom);
    }
    .close-section {
      padding-top: 1vw;
      display: flex;
      justify-content: center;
      flex-direction: column;
      align-items: center;
      .iconfont {
        color: #ffffff;
        opacity: 0.3;
      }
      view {
        font-size: 26rpx;
        font-family: PingFang SC;
        font-weight: 600;
        color: #ffffff;
        margin-top: 1.067vw;
        opacity: 0.3;
      }
    }
    .to-friend {
      margin-top: 1.34vw;
      text-align: center;
      font-size: 40rpx;
      font-family: PingFang SC;
      font-weight: 600;
      color: #ffffff;
    }
    .poster-img {
      width: 74.67vw;
      height: 101.33vw;
      background: #ffffff;
      box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.05);
      opacity: 1;
      border-radius: 2.67vw;
      margin: 2.67vw auto 4vw;
      position: relative;
      overflow: hidden;
      .share-img {
        width: 100%;
        height: 100%;
        display: block;
      }
      .download-box {
        width: 8.8vw;
        height: 8.8vw;
        border-radius: 50%;
        background-color: rgba(0, 0, 0, 0.7);
        display: flex;
        justify-content: center;
        align-items: center;
        position: absolute;
        right: 3.2vw;
        top: 3.73vw;
        z-index: 11;
        .iconfont {
          color: #ffffff;
          font-size: 44rpx;
        }
        .open-btn {
          width: 100%;
          height: 100%;
          opacity: 0;
          position: absolute;
          left: 0;
          top: 0;
          z-index: 12;
        }
      }
    }

    .poster-down-text {
      text-align: center;
      position: relative;
      font-size: 24rpx;
      font-family: PingFang SC;
      font-weight: 400;
      color: #8c8c8c;
    }

    .share-btn-group {
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-top: 4vw;
      .share-btn-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        margin-right: 5.33vw;
        position: relative;
        .item-icon-box {
          width: 12.8vw;
          height: 12.8vw;
          border-radius: 50%;
          background-color: #ffffff;
          display: flex;
          justify-content: center;
          align-items: center;
          .iconfont {
            font-size: 45rpx;
          }
        }
        .item-text {
          font-size: 22rpx;
          font-family: PingFang SC;
          font-weight: 400;
          color: #262626;
          margin-top: 2.13vw;
        }
        .wxshareBtn {
          width: 100%;
          height: 100%;
          position: absolute;
          left: 0;
          top: 0;
          z-index: 11;
          opacity: 0;
        }
      }
      .share-btn-item:last-child {
        margin-right: 0;
      }
    }
  }
}
</style>
